package com.QSMoney.service.impl;

import com.QSMoney.Utils.SecurityUtils;
import com.QSMoney.constants.ResultCode;
import com.QSMoney.entity.MoneyUser;
import com.QSMoney.entity.VO.LoginMoneyUserVo;
import com.QSMoney.entity.req.LoginMoneyUserReq;
import com.QSMoney.exception.BusinessException;
import com.QSMoney.mapper.MoneyUserMapper;
import com.QSMoney.entity.req.MoneyUserReq;
import com.QSMoney.service.ILoginService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class LoginServiceImpl extends ServiceImpl<MoneyUserMapper, MoneyUser> implements ILoginService {

    @Resource
    private MoneyUserMapper moneyUserMapper;
    @Override
    public LoginMoneyUserVo login(LoginMoneyUserReq loginMoneyUserReq) {
        String username=loginMoneyUserReq.getUsername();
        String password=loginMoneyUserReq.getPassword();
        // 参数校验
        if (username == null || password == null){
            throw new BusinessException(ResultCode.INCORRECT_PARAM, "用户名或密码不能为空");
        }
        // 用LambdaQueryWrapper查询
        LambdaQueryWrapper<MoneyUser> queryWrapper = new LambdaQueryWrapper<>();
        // 用户名匹配（eq = equal）
        queryWrapper.eq(MoneyUser::getUserName, username);
        MoneyUser user = moneyUserMapper.selectOne(queryWrapper);
        // 验证用户是否存在 + 密码是否匹配
        if (user == null) {
            throw new BusinessException(ResultCode.INCORRECT_PARAM, "用户不存在");
        }
        if (SecurityUtils.matchesPassword(password,user.getPassWord())) {
            Date now = new Date();
            user.setLoginDate(now);
            moneyUserMapper.updateById(user);

            LoginMoneyUserVo loginMoneyUserVo = new LoginMoneyUserVo();
            BeanUtils.copyProperties(user,loginMoneyUserVo);
            return loginMoneyUserVo;
        } else {
            throw new BusinessException(ResultCode.INCORRECT_PARAM, "用户名或密码错误");
        }
    }

    @Override
    public String register(MoneyUserReq moneyUserReq) {
        // 参数校验
        if (moneyUserReq == null){
            throw new BusinessException(ResultCode.INCORRECT_PARAM, "参数不能为空");
        }
        LambdaQueryWrapper<MoneyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MoneyUser::getUserName, moneyUserReq.getUserName());
        MoneyUser user = moneyUserMapper.selectOne(queryWrapper);

        if (user != null) {
            throw new BusinessException(ResultCode.INCORRECT_PARAM, "用户已存在");
        }
        MoneyUser moneyUser = new MoneyUser();
        moneyUser.setPassWord(SecurityUtils.encryptPassword(moneyUserReq.getPassWord()));
        moneyUser.setUserName(moneyUserReq.getUserName());
        Date now = new Date();
        moneyUser.setCreateTime(now);
        moneyUser.setUpdateTime(now);
        moneyUserMapper.insert(moneyUser);
        return "注册成功";
    }
}
